const app = getApp()
Page({
  data: {
    page: 1
  },
  detail(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/basics/food/detail?id=' + e.currentTarget.dataset.code
    })
  },
  loadMore: function (e) {
    var that = this;
    this.setData({
      isLoad: true
    })
    setTimeout(function () {
      if (that.data.category==0){
        console.log("pages", that.data)
        if (that.data.pages >= that.data.page) {
          wx.request({
            // url: 'https://ntdxyg.mynatapp.cc/cook/listByCategory',
            url: "http://" + app.globalData.url + "/cook/listByCategory/" + that.data.id + "/",
            method: 'GET',
            data: {
              "page": that.data.page
            },
            header: {
              'Accept': 'application/json'
            },
            success: function (res) {
              that.data.page += 1
              console.log("aaaaa", res.data.pages, that.data.page)
              that.setData({
                isLoad: false,
                list: that.data.list.concat(res.data.lists)
              })
            }
          })
        }else{
          wx.showToast({
            title: '到底了哦',
          })
        }

      }else{
        // 判断是否到最后一页了
        if (that.data.pages >= that.data.page){
          wx.request({
            // url: 'https://ntdxyg.mynatapp.cc/cook/listByKeyname',
            url: "http://" + app.globalData.url + "/cook/listByKeyname" + "/",
            method: 'GET',
            data: {
              "keywords": that.data.keyname,
              "page": (++that.data.page)
            },
            header: {
              'Accept': 'application/json'
            },
            success: function (res) {
              console.log("that.data.list.concat(res.data.data)", that.data.list.concat(res.data))
              that.setData({
                isLoad: false,
                list: that.data.list.concat(res.data.lists)
              })
            }
          })
        }else{
          wx.showToast({
            title: '到底了哦',
          })
        }

      }
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      name: options.name,
      category: options.category,
      keyname: options.keyname
    })
    var that = this;
    if (options.category == 0) {
      console.log("page", that.data)
      //分类查找
      wx.request({
    
        // url: 'https://ntdxyg.mynatapp.cc/cook/listByCategory',
        url: "http://" + app.globalData.url + "/cook/listByCategory/" + options.id + "/",
        method: 'GET',
        data: {
          "page": that.data.page
        },
        header: {
          'Accept': 'application/json'
        },
        success: function (res) {
          that.data.page += 1
          console.log(res.data)
          that.data.category = 0
          that.setData({
            list: res.data.lists,
            pages: res.data.pages
    
          })
        }
      })
    } else {
      //关键词查找
      console.log("keyname", options.keyname, options.name, that.keyname)
      wx.request({
        // url: 'https://ntdxyg.mynatapp.cc/cook/listByKeyname' + "/",
        url: "http://" + app.globalData.url + "/cook/listByKeyname" + "/",
        method: 'GET',
        data: {
          "keywords": options.keyname,
          "page": 1
        },
        header: {
          'Accept': 'application/json'
        },
        success: function (res) {
          console.log(res.data.data)
          that.setData({
            list: res.data.lists,
            pages: res.data.pages
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      console.log("下拉")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    console.log("optionsid", that.data.id, that.data.pages, that.data.Page)
    // 判断是获取菜品还是获取分类
    if (!that.data.id){
      console.log("pages, page", that.data.pages, that.data.page)
      if(that.data.pages >= that.data.page){
        wx.request({
          // url: 'https://ntdxyg.mynatapp.cc/cook/listByKeyname',
          url: "http://" + app.globalData.url + "/cook/listByKeyname" + "/",
          method: 'GET',
          data: {
            "keywords": that.data.keyname,
            "page": (++that.data.page)
          },
          header: {
            'Accept': 'application/json'
          },
          success: function (res) {
            console.log("aaaa到底了", res.data)
            if (!res.data.detail){
              that.setData({
                isLoad: false,
                list: that.data.list.concat(res.data.lists)
              })
            }else{
              wx.showToast({
                title: '到底了',
              })
            }
    
          }
        })
      }else{
        wx.showToast({
          title: '到底了',
        })
      }
    }else{
      if(that.data.pages >= that.data.page){
      wx.request({
        // url: 'https://ntdxyg.mynatapp.cc/cook/listByCategory',
        url: "http://" + app.globalData.url + "/cook/listByCategory/" + that.data.id + "/",
        method: 'GET',
        data: {
          "page": that.data.page
        },
        header: {
          'Accept': 'application/json'
        },
        success: function (res) {
          that.data.page += 1
          console.log(res.data)
          that.data.category = 0
          that.setData({
            isLoad: false,
            list: that.data.list.concat(res.data.lists)
    
          })
        }
      })
    }else{
      wx.showToast({
        title: '到底了',
      })
    }
    }


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})